import { createRouter, createWebHistory } from "vue-router";
import api from "../axios/api";

export const routes = [
  {
    path: "/",
    name: "main",
    component: () => import("@/pages/Main.vue"),
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "home",
        meta: { title: "首页" },
        component: () => import("@/pages/Home.vue")
      },
      {
        path: "/myCourse",
        name: "myCourse",
        meta: { title: "我的课程" },
        component: () => import("@/pages/MyCourse.vue")
      },
      {
        path: "/course",
        name: "course",
        meta: { title: "自动排课" },
        component: () => import("@/pages/Course.vue")
      },
      {
        path: "/CourseRecommendation",
        name: "CourseRecommendation",
        meta: { title: "课程推荐" },
        component: () => import("@/pages/CourseRecommendation.vue")
      },
      {
        path: "/myorganization",
        name: "myorganization",
        meta: { title: "我的组织" },
        component: () => import("@/pages/MyOrganization.vue")
      },
      {
        path: "/test",
        name: "test",
        meta: { title: "测试" },
        component: () => import("@/pages/Test.vue")
      },
      {
        path: "/individual",
        name: "individual",
        meta: { title: "个人中心" },
        component: () => import("@/pages/IndividualCenter.vue")
      },
      {
        path: "/MyOrgInfo/:orgId",
        name: "MyOrgInfo",
        meta: { title: "组织信息" },
        component: () => import("@/pages/MyOrgInfo.vue")
      },
      {
        path: "/SchedulingWork/:orgId",
        name: "SchedulingWork",
        meta: { title: "排班" },
        component: () => import("@/pages/SchedulingWork.vue")
      },
      {
        path: "/SchedulingCourse",
        name: "SchedulingCourse",
        meta: { title: "排课" },
        component: () => import("@/pages/SchedulingCourse.vue")
      },
      {
        path: "/feedback",
        name: "feedback",
        meta: { title: "反馈" },
        component: () => import("@/pages/Feedback.vue")
      },
      {
        path: "/updatelogs",
        name: "updatelogs",
        meta: { title: "更新日志管理" },
        component: () => import("@/pages/UpdateLogs.vue")
      },
      {
        path: "/teachers",
        name: "teachers",
        meta: { title: "教师工号" },
        component: () => import("@/pages/Teachers.vue")
      },
      {
        path: "/school-calendar",
        name: "schoolCalendar",
        meta: { title: "校历管理" },
        component: () => import("@/pages/SchoolCalendarList.vue")
      }
    ]
  },
  {
    path: "/login",
    name: "login",
    meta: { title: "登录" },
    component: () => import("@/pages/Login.vue")
  },
  {
    path: "/register",
    name: "register",
    meta: { title: "注册" },
    component: () => import("@/pages/Register.vue")
  },
  {
    path: "/changePassword",
    name: "changePassword",
    meta: { title: "修改密码" },
    component: () => import("@/pages/updatePassword.vue")
  },
  {
    path: "/unsupported",
    name: "UnsupportedDevice",
    component: () => import("../components/UnsupportedDevice.vue")
  }
];
const router = createRouter({
  scrollBehavior: () => ({ left: 0, top: 0 }),
  history: createWebHistory(),
  routes
});
router.beforeEach(async (to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.name === "login" || to.name === "register") {
    next();
  } else if (!token) {
    next({ name: "login" });
  } else {
    try {
      const response = await api.verifyUser();
      if (response.status !== 200) {
        localStorage.clear();
        next({ name: "login" });
      } else {
        // 检查管理员权限
        const adminOnly = ["individual", "feedback", "updatelogs", "teachers", "schoolCalendar"];
        const userRole = localStorage.getItem("role");
        if (adminOnly.includes(to.name as string) && userRole !== "admin") {
          next({ name: "home" });
        } else {
          next();
        }
      }
    } catch (error) {
      console.error("验证用户失败", error);
      next({ name: "login" });
    }
  }
});

export default router;
